package com.cc.controller;


import com.cc.model.EbUser;
import com.cc.service.UserService;
import com.cc.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/list")
public ModelAndView list(@RequestParam(value = "page" ,defaultValue = "1") int page,
@RequestParam(value = "pageSize" ,defaultValue = "3") int pageSize) {
        List<EbUser> users = userService.list(page, pageSize);

        int maxPage = userService.getMaxPage(pageSize);

        ModelAndView mv = new ModelAndView();

        mv.addObject("users", users);
        //绑定当前页
        mv.addObject("curPage", page);
        //b绑定最大页
        mv.addObject("maxPage", maxPage);
        //绑定视图
        mv.setViewName("manage/user");

        return mv;
        }
@RequestMapping("save")
public String save()  {
        return "manage/user-add";
        }

@RequestMapping("user-result")
public String UserResult(EbUser ebUser){
       /* System.out.println(ebUser.getEu_UserId());
        String uu = UUID.randomUUID().toString();
        System.out.println(uu);*/
//        ebUser.setEu_UserId(uu);

        System.out.println("++++++++++++++"+ebUser);

        if(ebUser.getEu_UserName()==null||
        ebUser.getEu_UserName().equals("")||
        ebUser.getEu_UserName().trim().length()<0||
        ebUser.getEu_Password()==null||
        ebUser.getEu_Password().equals("")||
        ebUser.getEu_Password().trim().length()<0||
        ebUser.getEu_Sex()==null||
        ebUser.getEu_Sex().equals("")||
        ebUser.getEu_Sex().trim().length()<0){
        return "manage/manage-error";

        }
        int result = userService.save(ebUser);
        if(result<=0){

        }
        return "manage/manage-result";
        }

@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") String id) {
        int result = userService.deleteByPk(id);
        System.out.println(result);
        if(result==0){

        }

        return "manage/manage-result1";
    }
    @RequestMapping("/edit")
    public String edit(Model model, String euUserId){

        EbUser ebUser = userService.selectByCondition(euUserId);
        System.out.println("================="+ebUser);
        model.addAttribute("ebUser",ebUser);
        return "manage/user-modify";
        }
@RequestMapping("/edit-result")
public String editResult(EbUser ebUsers) {
    System.out.println("=================" + ebUsers);
    int result = userService.updateById(ebUsers);
    System.out.println(result);
    if (result <= 0) {
        return "manage/manage-error";
    }
    return "manage/manage-result";
}
    @RequestMapping("/to_login")
    public String toLogin() {
        return "login";
    }
    @RequestMapping("/login")
    public String login(String userName, String passWord, HttpServletRequest request) {
        System.out.println("userName=" + userName + "\tpassWord=" + passWord);
        HttpSession session = request.getSession();
        session.removeAttribute("lgn_error");
        //1.查找数据库是否存在这个用户
        //1.1 得到密码的密文
        String md5Pwd = MD5Util.MD5EncodeUtf8(passWord);
        //1.2
        EbUser loginUser = userService.checkUser(userName, md5Pwd);
        System.out.println("loginUser:===" + loginUser);
        if (loginUser == null) {
            session.setAttribute("lgn_error", "登录失败");
            //重定向
            return "redirect:to_login";
        }
        //2.将视图告诉spring mvc中处理器
        //2.1 要将用户信息放到session
        session.setAttribute("lgnUser", loginUser);
        //跳转到网站的前台首页
        return "index";
    }

    @RequestMapping("/to_register")
    public String toRegister() {
        return "register";
    }

}
